import axios from 'axios';
import qs from 'query-string';

export interface CardItemId {
    id: number;
}

export interface CardItemRecord {
    id: number;
    card_id: number;
    card_number: string;
    password: string;
    expired_at: number;
    status: number;
    status_text: string;
    score: number;
    created_at: number;
    updated_at: number;
}

export interface CardItemListRes {
    list: CardItemRecord[];
    total: number;
    page: number;
    limit: number;
}

export interface CardItemModel {
    id: number;
    card_id: number;
    card_number?: string;
    password?: string;
    expired_at?: number;
    score?: number;
}

export interface CardItemParams {
    card_id: number;
    page?: number;
    limit?: number;
    status?: number;
    card_number?: string;
}

/**
 * 获取卡数据列表
 * @param params
 */
export function queryCardItemList(params: CardItemParams) {
    return axios.get<CardItemListRes>('/admin/cardItem/index', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取卡数据详情
 * @param params
 */
export function queryCardItemOne(params: CardItemId) {
    return axios.get<CardItemRecord>('/admin/cardItem/detail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 提交卡数据表单（创建/更新）
 * @param data
 */
export function submitCardItemForm(data: CardItemModel) {
    return axios.post('/admin/cardItem/submit', data);
}

/**
 * 删除卡数据
 * @param id
 */
export function delCardItem(id: number) {
    return axios.post('/admin/cardItem/delete', { id });
}
